package com.example.demo.service;

import com.example.demo.entity.Student;
import com.example.demo.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StudentService {

    @Autowired
    private  StudentMapper studentMapper;

    public Map<String, Object> studentRegist(@org.jetbrains.annotations.NotNull Student student) {
        Student studentOld = studentMapper.getByStudentIdOrName(student.getStudentId(), student.getUsername());
        Map<String, Object> retData = new HashMap<>();
        if (studentOld != null) {
            retData.put("code", 500);
            retData.put("message", "用户名或密码重复");
        } else {
            studentMapper.insertStudent(student);
            retData.put("code", 200);
            retData.put("message", "注册成功！");
        }
        return retData;
    }

    public Map<String, Object> searchStudents(Map<String, Object> params) {
        List<Student> students = studentMapper.getStudent(params);
        Map<String, Object> retData = new HashMap<>();
        if (students != null && !students.isEmpty()) {
            retData.put("code", 200);
            retData.put("message", "查询成功");
            List<Map<String, Object>> resultList = new ArrayList<>();
            for (Student student : students) {
                Map<String, Object> studentInfo = new HashMap<>();
                studentInfo.put("username", student.getUsername());
                studentInfo.put("name", student.getName());
                studentInfo.put("major", student.getMajor());
                studentInfo.put("classId", student.getClassId());
                studentInfo.put("studentId", student.getStudentId());
                resultList.add(studentInfo);
            }
            retData.put("data", resultList);
        } else {
            retData.put("code", 500);
            retData.put("message", "未找到符合条件的学生");
        }
        return retData;
    }

    public Map<String, Object> deleteStudent(Map<String, Object> params) {
        String username = (String) params.get("username");
        if (username == null || username.isEmpty()) {
            Map<String, Object> retData = new HashMap<>();
            retData.put("code", 500);
            retData.put("message", "参数错误！");
            return retData;
        } else {
            studentMapper.deleteStudent(username);
            Map<String, Object> retData = new HashMap<>();
            retData.put("code", 200);
            retData.put("message", "删除成功！");
            return retData;
        }
    }

    public Map<String, Object> updateStudent(Student student) {
        Student studentOld = studentMapper.getByStudentIdOrName(student.getStudentId(), student.getUsername());
        Map<String, Object> retData = new HashMap<>();
        if (studentOld == null) {
            retData.put("code", 500);
            retData.put("message", "学生不存在！");
        } else {
            studentMapper.updateStudent(student);
            retData.put("code", 200);
            retData.put("message", "修改成功！");
        }
        return retData;
    }

}



